package com.yfux._05_tlias.util;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Slf4j
@Component
public class OssUtil {
    private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    private String ossAccessKeyId = "LTAI5tBrmygBcz5wTUM62bqa";
    private String ossAccessKeySecret = "fMYBNUBhmY1mN7H3e4lKcxxcQkVgNk";
    private String bucketName = "tlias-dev12";
    private String region = "cn-hangzhou";

    public String uploadFile(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String objectName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        // 创建OSSClient实例
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(new DefaultCredentialProvider(ossAccessKeyId, ossAccessKeySecret))
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            InputStream inputStream = file.getInputStream();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            String imageUrl = "https://" + bucketName + "." + endpoint.substring(endpoint.indexOf("//") + 2) + "/" + objectName;
            log.info("上传的文件路径为: {}", imageUrl);
            return imageUrl;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
            log.error("文件{}上传出现了异常", originalFilename);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件{}上传出现了异常", originalFilename);
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
